
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Activation
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.http

<Serializable()> Public Class Subscriber
    Inherits MarshalByRefObject

    ' A subscriber should have a list of all its subscriptions so that it
    ' can add or remove them as needed.
    Public Subscriptions As New ArrayList()

    Public Sub New()
        ' We need to have a client channel ready in order to subscribe
        ' to publications.  An exception is thrown when more than one
        ' client channel is registered.  Each local publisher object
        ' needs its own client channel, and we don't know if a publisher
        ' has already asked for one.  We also can't assume that a
        ' channel will already be allocated, because a subscription can
        ' exist without a local publisher.  So we need to encase the
        ' attempt inside of a try catch block.
        Try
            ' We need to have a client channel ready in order to subscribe
            ' to publications.
            ChannelServices.RegisterChannel(New HttpClientChannel)
        Catch
            ' We don't care if the registration failed.  It probably means
            ' that a publisher has already asked for a client channel.  We
            ' will be able to "piggy-back" on the publisher's client channel.
        End Try
    End Sub

    ' Client code calls this function when it would like to subscribe to
    ' a remote publication through the use of a U.R.I. remoting object.
    Public Sub Subscribe(ByVal URI As String)
        ' Create a new subscription object using the U.R.I. object.  We pass
        ' ourselves (using Me) for reasons documented in the code of the
        ' subscription object.
        Dim Subscription As New Subscription(URI, Me)
        ' Add an event handler to the subscription to handle when an issue
        ' of the publication is produced by the publisher.
        AddHandler Subscription.ReceiveIssue, AddressOf Me.ReceiveIssue

        ' Add the new subscription to our internal list.
        Me.Subscriptions.Add(Subscription)
    End Sub

    ' This event set passes an issue event from a subscription to any client
    ' code object that is listening to us.
    Public Event ReceivedIssue(ByVal Issue As Object)
    Public Sub ReceiveIssue(ByVal Publication As Publication, ByVal Issue As Object)
        RaiseEvent ReceivedIssue(Issue)
    End Sub
End Class
